package xcmg.device.web.userInfo;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanyDealerService;
import xcmg.device.service.crm.util.ActionResultUtil;
import xcmg.device.service.userinfo.LoginInfoService;
import xcmg.device.service.vo.userinfo.AbnormalLoginVO;
import xcmg.device.service.vo.userinfo.AgentLoginLogsQueryVO;
import xcmg.device.service.vo.userinfo.LoginLogQeuryParamVO;
import xcmg.device.service.vo.userinfo.LoginLogQueryResultVO;
import xcmg.device.service.vo.userinfo.LoginLogStatisticVO;
import xcmg.device.service.vo.userinfo.UserLoginLogDetailsQueryParamVO;
import yb.ecp.fast.dto.UserDetailDTO;
import yb.ecp.fast.feign.AuthClient;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;

@RestController
@RequestMapping("logininfo")
public class LoginInfoController extends BasicController {
    @Autowired
    private LoginInfoService loginInfoService;
    @Autowired
    private AuthClient authClient;
    @Autowired
    private CompanyDealerService companyDealerService;
    @Autowired
    private HttpServletResponse response;

    @RequestMapping(value="loginlogs", method = RequestMethod.GET)
    public ActionResult getUserLoginLogs(SearchCommonVO<LoginLogQeuryParamVO> condition) throws Exception {
        //
        LoginLogQeuryParamVO filters = condition.getFilters();
        if(filters == null){
            filters = new LoginLogQeuryParamVO();
        }
        filters.setSpaceId(getOrgId());
        condition.setFilters(filters);
        PageCommonVO<LoginLogQueryResultVO> userLoginLogs = loginInfoService.getUserLoginLogs(condition);
        return ActionResultUtil.successActionResult(userLoginLogs.getPageInfo());
    }

    /**
     * @Des 本公司人员登录统计信息
     * @Date 2021/4/2 14:27
     * @Author wangzhaoyu
     * @Param [condition]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @FastMappingInfo(needLogin = true)
    @RequestMapping(value = "statisticsInfo", method = RequestMethod.POST)
    public ActionResult getStatisticsInfo(@RequestBody SearchCommonVO<LoginLogQeuryParamVO> condition) throws Exception {
        LoginLogQeuryParamVO filters = condition.getFilters();
        if(filters == null){
            filters = new LoginLogQeuryParamVO();
        }
        filters.setSpaceId(getOrgId());
        condition.setFilters(filters);
        PageCommonVO<LoginLogStatisticVO> userLoginLogs = loginInfoService.getUserLoginStatics(condition);
        return ActionResultUtil.successActionResult(userLoginLogs.getPageInfo());
    }

    /**
     * @Des 本公司人员登录统计信息导出
     * @Date 2021/4/7 17:45
     * @Author wangzhaoyu
     * @Param [filter]
     * @Return void
     */
    @FastMappingInfo(needLogin = true)
    @RequestMapping(value = "statisticsInfoExportAll", method = RequestMethod.POST)
    public void statisticsInfoExportAll( LoginLogQeuryParamVO filter) throws Exception {
        String orgId = getOrgId();
        filter.setSpaceId(orgId);
        loginInfoService.userLoginStaticsExport(filter,response);
    }

    /**
     * @Des 本公司人员登录详情信息导出
     * @Date 2021/4/7 17:45
     * @Author wangzhaoyu
     * @Param [filter]
     * @Return void
     */
    @FastMappingInfo(needLogin = true)
    @RequestMapping(value = "userLoginDetailExportAll", method = RequestMethod.POST)
    public void userLoginDetailExportAll( LoginLogQeuryParamVO filter) throws Exception {
        String orgId = getOrgId();
        filter.setSpaceId(orgId);
        loginInfoService.userLoginDetailExport(filter,response);
    }

    /**
     * @Des 单个用户的登录日志详情记录
     * @Date 2021/4/2 14:38
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "userLogs" , method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult getLoginLogs(@RequestBody SearchCommonVO<UserLoginLogDetailsQueryParamVO> condition){
        UserLoginLogDetailsQueryParamVO filters = condition.getFilters();
        if(filters == null){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument);
        }
        PageCommonVO<LoginLogQueryResultVO> userLoginLogs = loginInfoService.getloginDetails(condition);
        return ActionResultUtil.successActionResult(userLoginLogs);
    }

    /**
     * @Des 获取异常登录的信息：
     * 思路：使用最简单的逻辑，登录成功后调用接口，查询最后两条，粗略认为最新的一条为当前用户登录的ip,倒数第二条为当前用户前一次登录的记录，
     * 如果最后两条的ip不同，则认为倒数第二条为异常的登录记录
     * @Date 2021/4/7 11:48
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "abnormalLogin" , method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult abnormalLogin(){
        String userId = getUserId();
        //根据userId查询最后两条登录记录
        AbnormalLoginVO result = loginInfoService.getAbnormalLogin(userId);
        return ActionResultUtil.successActionResult(result);
    }

    /**
     * @Des 经销商登录统计信息
     * @Date 2021/4/7 15:08
     * @Author wangzhaoyu
     * @Param [condition]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "agentLoginStatisticsInfo" , method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult agentLoginStatisticsInfo(@RequestBody SearchCommonVO<AgentLoginLogsQueryVO> condition) throws Exception {
        AgentLoginLogsQueryVO filters = condition.getFilters();
        if(filters == null){
            filters = new AgentLoginLogsQueryVO();
            condition.setFilters(filters);
        }
        String orgId = getOrgId();
        filters.setOrgId(orgId);
        PageCommonVO<LoginLogStatisticVO> agentLoginStaticPage = loginInfoService.getAgentLoginStatics(condition);

        return ActionResultUtil.successActionResult(agentLoginStaticPage.getPageInfo());
    }

    /**
     * @Des 代理商单个用户登录的详情记录
     * @Date 2021/4/7 16:56
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "agentUserLogs" , method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult getAgentLoginLogs(@RequestBody SearchCommonVO<UserLoginLogDetailsQueryParamVO> condition) throws Exception {
        //判断需要查看的用户是否能被当前用户查看
        UserLoginLogDetailsQueryParamVO filters = condition.getFilters();
        String userId = filters.getUserId();
        if(StringUtils.isEmpty(userId)){
            return ActionResultUtil.successActionResult(new PageInfo(Collections.emptyList()));
        }
        //获取用户所属的组织机构，查看用户所属的组织机构是否为当前登录所在机构的代理商
        ActionResult<UserDetailDTO> userDetailResult = authClient.getUserDetail(userId);
        if(userDetailResult == null){
            return ActionResultUtil.successActionResult(new PageInfo(Collections.emptyList()));
        }
        UserDetailDTO userDetail = userDetailResult.getValue();
        if(userDetail == null){
            return ActionResultUtil.successActionResult(new PageInfo(Collections.emptyList()));
        }
        String spaceId = userDetail.getSpaceId();
        if(StringUtils.isNotEmpty(spaceId)){
            String orgId = getOrgId();
            //
            Integer count = companyDealerService.countDealerByOrgIdAndPlatOrgId(orgId, spaceId);
            if(count <= 0){
                return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"没有权限查看此客户");
            }
        }
        PageCommonVO<LoginLogQueryResultVO> userLoginLogs = loginInfoService.getloginDetails(condition);
        return ActionResultUtil.successActionResult(userLoginLogs);
    }

    /**
     * @Des 经销商登录统计信息导出
     * @Date 2021/4/7 15:08
     * @Author wangzhaoyu
     * @Param [condition]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "agentLoginStatisticsExportAll" , method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void agentLoginStatisticsExportAll( AgentLoginLogsQueryVO filter) throws Exception {
        String orgId = getOrgId();
        filter.setOrgId(orgId);
        loginInfoService.agentLoginStaticsExportAll(filter,response);
    }


    /**
     * @Des 经销商登录详情导出
     * @Date 2021/4/7 15:08
     * @Author wangzhaoyu
     * @Param [condition]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "agentLoginDetailsExportAll" , method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void agentLoginDetailsExportAll( AgentLoginLogsQueryVO filter) throws Exception {
        String orgId = getOrgId();
        filter.setOrgId(orgId);
        loginInfoService.agentLoginDetailsExportAll(filter,response);
    }
}
